<template>
  <div id="upLoadImg">
    <!-- http://localhost:3000/images/uploadImg -->

    <el-upload
      action="http://localhost:3000/images/uploadImg"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :on-error="handleError"
      :before-upload='beforeUpload'
      :limit="1"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState: imgMapState, mapMutations: imgMapMutations } =
  createNamespacedHelpers("img");

export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
    };
  },
  methods: {
    ...imgMapMutations(["changeFilename",'changeIsShow']),

    beforeUpload(file){
      console.log('beforeUpload--',file);
    },
    handleRemove(file) {
      console.log(file);
    },
    //弹窗的函数
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    handleSuccess(response, file, fileList) {
      if (response.status) {
        console.log(response);
        this.changeFilename(response.filename);
        this.$message({
          message: response.message,
          type: "success",
        });
        //只针对修改学生组件，不影响其他组件
        this.changeIsShow()
      } else {
        console.log(response);
      }
    },
    handleError(err, file, fileList) {
      console.log(err);
    },
  },
};
</script>

<style>
</style>